home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / shllutil.lha / shellutils-1.8 / src / echo.c < prev    next >
C/C++ Source or Header  |  1992-10-28  |  4KB  |  180 lines

  1. /* echo.c, taken from Bash.
  2. Copyright (C) 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Bash, the Bourne Again SHell.
  5.  
  6. Bash is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 2, or (at your option) any later
  9. version.
  10.  
  11. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License along
  17. with Bash; see the file COPYING.  If not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include "system.h"
  23.  
  24. /* echo [-neE] [arg ...]
  25. Output the ARGs.  If -n is specified, the trailing newline is
  26. suppressed.  If the -e option is given, interpretation of the
  27. following backslash-escaped characters is turned on:
  28.     \a    alert (bell)
  29.     \b    backspace
  30.     \c    suppress trailing newline
  31.     \f    form feed
  32.     \n    new line
  33.     \r    carriage return
  34.     \t    horizontal tab
  35.     \v    vertical tab
  36.     \\    backslash
  37.     \num    the character whose ASCII code is NUM (octal).
  38.  
  39. You can explicitly turn off the interpretation of the above characters
  40. on System V systems with the -E option.
  41. */
  42.  
  43. #define V9_ECHO
  44.  
  45. #if defined (V9_ECHO)
  46. #  if defined (USG)
  47. #    define VALID_ECHO_OPTIONS "neE"
  48. #  else
  49. #    define VALID_ECHO_OPTIONS "ne"
  50. #  endif /* !USG */
  51. #else /* !V9_ECHO */
  52. #  define VALID_ECHO_OPTIONS "n"
  53. #endif /* !V9_ECHO */
  54.  
  55. /* Print the words in LIST to standard output.  If the first word is
  56.    `-n', then don't print a trailing newline.  We also support the
  57.    echo syntax from Version 9 unix systems. */
  58. void
  59. main (argc, argv)
  60.      int argc;
  61.      char **argv;
  62. {
  63.   int display_return = 1, do_v9 = 0;
  64.  
  65. /* System V machines already have a /bin/sh with a v9 behaviour.  We
  66.    use the identical behaviour for these machines so that the
  67.    existing system shell scripts won't barf. */
  68. #if defined (V9_ECHO) && defined (USG)
  69.   do_v9 = 1;
  70. #endif
  71.  
  72.   --argc;
  73.   ++argv;
  74.  
  75.   while (argc > 0 && *argv[0] == '-')
  76.     {
  77.       register char *temp;
  78.       register int i;
  79.  
  80.       /* If it appears that we are handling options, then make sure that
  81.      all of the options specified are actually valid.  Otherwise, the
  82.      string should just be echoed. */
  83.       temp = argv[0] + 1;
  84.  
  85.       for (i = 0; temp[i]; i++)
  86.     {
  87.       if (rindex (VALID_ECHO_OPTIONS, temp[i]) == 0)
  88.         goto just_echo;
  89.     }
  90.  
  91.       if (!*temp)
  92.     goto just_echo;
  93.  
  94.       /* All of the options in TEMP are valid options to ECHO.
  95.      Handle them. */
  96.       while (*temp)
  97.     {
  98.       if (*temp == 'n')
  99.         display_return = 0;
  100. #if defined (V9_ECHO)
  101.       else if (*temp == 'e')
  102.         do_v9 = 1;
  103. #if defined (USG)
  104.       else if (*temp == 'E')
  105.         do_v9 = 0;
  106. #endif /* USG */
  107. #endif /* V9_ECHO */
  108.       else
  109.         goto just_echo;
  110.  
  111.       temp++;
  112.     }
  113.       argc--;
  114.       argv++;
  115.     }
  116.  
  117. just_echo:
  118.  
  119.   if (argc > 0)
  120.     {
  121. #if defined (V9_ECHO)
  122.       if (do_v9)
  123.     {
  124.       while (argc > 0)
  125.         {
  126.           register char *s = argv[0];
  127.           register int c;
  128.  
  129.           while (c = *s++)
  130.         {
  131.           if (c == '\\' && *s)
  132.             {
  133.               switch (c = *s++)
  134.             {
  135.             case 'a': c = '\007'; break;
  136.             case 'b': c = '\b'; break;
  137.             case 'c': display_return = 0; continue;
  138.             case 'f': c = '\f'; break;
  139.             case 'n': c = '\n'; break;
  140.             case 'r': c = '\r'; break;
  141.             case 't': c = '\t'; break;
  142.             case 'v': c = (int) 0x0B; break;
  143.             case '0': case '1': case '2': case '3':
  144.             case '4': case '5': case '6': case '7':
  145.               c -= '0';
  146.               if (*s >= '0' && *s <= '7')
  147.                 c = c * 8 + (*s++ - '0');
  148.               if (*s >= '0' && *s <= '7')
  149.                 c = c * 8 + (*s++ - '0');
  150.               break;
  151.             case '\\': break;
  152.             default:  putchar ('\\'); break;
  153.             }
  154.             }
  155.           putchar(c);
  156.         }
  157.           argc--;
  158.           argv++;
  159.           if (argc > 0)
  160.         putchar(' ');
  161.         }
  162.     }
  163.       else
  164. #endif /* V9_ECHO */
  165.     {
  166.       while (argc > 0)
  167.         {
  168.           fputs (argv[0], stdout);
  169.           argc--;
  170.           argv++;
  171.           if (argc > 0)
  172.         putchar (' ');
  173.         }
  174.     }
  175.     }
  176.   if (display_return)
  177.     printf ("\n");
  178.   exit (0);
  179. }
  180.